package firsttest.test.fragment2;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;

public class FragmentB extends Fragment {

    private Button btn;
    private TextView txt_a;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragmentb_layout, null);
        initView(inflate);
        return inflate;
    }

    private void initView(View inflate) {
        btn = inflate.findViewById(R.id.btnB);
        txt_a = inflate.findViewById(R.id.txt_b);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击b的按钮，修改activity的信息
                //先得到activity对象
                MainActivity activity = (MainActivity) getActivity();
                activity.setmTxtTitle("来自于fragment的信息");
            }
        });
    }
    public void setName(String newStr){//修改name的组件信息
        txt_a.setText(newStr);
    }
}
